package com.example.springbootrbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootrbac.entity.*;
import com.example.springbootrbac.repository.*;
import com.example.springbootrbac.service.InsertService;
import com.example.springbootrbac.service.UpdateService;
import com.example.springbootrbac.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class InsertServiceImpl implements InsertService {
    @Resource(name = "announcementRepository")
    AnnouncementRepository announcementRepository;
    @Resource(name = "roomRepository")
    RoomRepository roomRepository;
    @Resource(name = "buildingRepository")
    BuildingRepository buildingRepository;
    @Resource(name = "courseRepository")
    CourseRepository courseRepository;
    @Resource(name = "messagesBoardRepository")
    MessagesBoardRepository messagesBoardRepository;
    @Resource(name = "userServiceImpl")
    UserService userService;
    @Resource(name = "userRepository")
    UserRepository userRepository;
    @Resource(name = "teacherRepository")
    TeacherRepository teacherRepository;

    @Override
    public String insertUser(UserEntity user, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity user1 = (UserEntity) session.getAttribute("user");
        UserEntity user2 = userRepository.selectOne(
                new QueryWrapper<UserEntity>()
                        .eq("username", user.getUsername())
        );
        if (ObjectUtils.isEmpty(user2))
            userRepository.insert(user);
        return "token:ok!";
    }

    @Override
    public String insetAnnouncement(AnnouncementEntity announcement, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserEntity user = (UserEntity) session.getAttribute("user");
        if (announcement.getTitle().length() < 20) {
            announcement.setCreateBy(user.getUsername());
            announcement.setCreateTime(LocalDateTime.now());
            announcementRepository.insert(announcement);
            return "token:公告发布成功";
        }
        return "1401";
    }

    @Override
    public String insertRoom(RoomEntity room, String buildingName, HttpServletRequest request) {
        List<BuildingEntity> buildingEntityList = buildingRepository.selectList(
                new QueryWrapper<BuildingEntity>()
                        .select("id")
                        .eq("name", buildingName)
        );
        List<RoomEntity> roomEntityList = roomRepository.selectList(
                new QueryWrapper<RoomEntity>()
                        .select("name")
                        .eq("name", room.getName())
        );
        if (!ObjectUtils.isEmpty(buildingEntityList) && ObjectUtils.isEmpty(roomEntityList)) {
            if (ObjectUtils.isEmpty(room.getQuality())) {
                room.setQuality("暂定");
            }
            room.setBelong(buildingEntityList.get(0).getId());
            HttpSession session = request.getSession();
            UserEntity user = (UserEntity) session.getAttribute("user");
            room.setCreateBy(user.getUsername());
            room.setCreateTime(LocalDateTime.now());
            roomRepository.insert(room);
            return "token:房屋信息添加成功";
        }
        return null;
    }

    @Override
    public String insertCourse(CourseEntity course, HttpServletRequest request) {
        List<CourseEntity> courseEntityList = courseRepository.selectList(  //查找是否已经存在相同的课（目前只根据写入的三点判别）
                new QueryWrapper<CourseEntity>()
                        .select("name")
                        .eq("name", course.getName())
                        .eq("teacher",course.getTeacher())
                        .eq("address",course.getAddress())
        );
        //排查外码
        TeacherEntity test_teacher = teacherRepository.selectOne(
                new QueryWrapper<TeacherEntity>()
                        .eq("tname",course.getTeacher())
        );
        RoomEntity test_room = roomRepository.selectOne(
                new QueryWrapper<RoomEntity>()
                        .eq("name",course.getAddress())
        );
        if(ObjectUtils.isEmpty(test_room)  ){
            return "FK_ERROR_1";
        }else if(ObjectUtils.isEmpty(test_teacher)){
            return "FK_ERROR_2";
        }
        if (ObjectUtils.isEmpty(courseEntityList)) {   //不存在相同的课程
            HttpSession session = request.getSession();
            UserEntity user = (UserEntity) session.getAttribute("user");
            course.setLength(48);
            course.setWeek("1~16");          //周期和课时在插入时暂且默认
            course.setCreateBy(user.getUsername());
            course.setCreateTime(LocalDateTime.now());
            courseRepository.insert(course);
            return "token:课程添加成功";
        }
        return null;
    }

    @Override
    public String insertMessageBoard(MessagesBoardEntity messagesBoard, HttpServletRequest request) {
        if (messagesBoard.getText().length() <= 50) {
            HttpSession session = request.getSession();
            UserEntity user = (UserEntity) session.getAttribute("user");
            messagesBoard.setCreateBy(user.getUsername());
            messagesBoard.setCreateTime(LocalDateTime.now());
            messagesBoardRepository.insert(messagesBoard);
            return "token:留言发布成功";
        }
        return null;
    }
}
